/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package server.network;

import com.cse.p2p.common.CommonSettings;
import com.cse.p2p.common.DicFile;
import com.cse.p2p.common.Message;
import com.cse.p2p.database.ClientDatabase;
import com.cse.p2p.parser.PeerInfo;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import server.logic.ServerLogicImplement;

/**
 *
 * @author TuyetNgan
 * Ask other peer List of PIECES
 */
public class ServerControlTask_Client extends Thread {

	private int portControl;
	int portNumber = CommonSettings.CLIENT_SERVERSOCKET;
	private Socket clientSocket = null;
	private BufferedReader in = null;
	private PrintStream out = null;
	private String IPconnect;
	private Integer fileID = null;
	private PeerInfo peerInfo = null;
	private ServerControlTask_Server server;

	public ServerControlTask_Client( ServerControlTask_Server server, String ip, int p, Integer id ) {
		this.IPconnect = ip;
		this.portControl = p;
		this.fileID = id;
		this.server = server;
	}

	public void newChat() {
		try {
			clientSocket = new Socket( IPconnect, portControl );
			System.out.println( "NETWORK: CONTROL: Established a new connection to " + clientSocket.getRemoteSocketAddress() );
			out = new PrintStream( new DataOutputStream( clientSocket.getOutputStream() ) );
			in = new BufferedReader( new InputStreamReader( clientSocket.getInputStream() ) );
		}
		catch ( IOException e ) {
			System.err.println( "ERROR: NETWORK-CONTROL: Cannot ask info from " + IPconnect + ":" + portControl );
			e.printStackTrace();
		}
	}

	public void closeConnection() {
		try {
			out.close();
			in.close();
			clientSocket.close();
		}
		catch ( IOException e ) {
		}

	}

	public PeerInfo getData() {
		return peerInfo;
	}

	public void waiting() {

		try {
			String mess, data;
			Message message;
			mess = in.readLine();
			System.out.println( "NETWORK: CONTROL: New message of " + fileID + " arrives from " + clientSocket.getRemoteSocketAddress() + ". Analizing ...");
			message = Message.fromString( mess );
			int typeOfProtocol = message.getType();
			data = message.getData();

			switch ( typeOfProtocol ) {
				//one Peer -> Server: the info of all available pieces of a file
				case Message.MsgType.RES_VRF_ALL_PIECES: {
					peerInfo = PeerInfo.fromString( data );
					System.out.println( "NETWORK: CONTROL: Recive info of file " + fileID + " from " + clientSocket.getRemoteSocketAddress() );
					break;
				}

			}
		}
		catch ( Exception ex ) {
			System.out.println( "ERROR: Server request piece info" );
			ex.printStackTrace();
		}
	}

	@Override
	public void run() {
		newChat();
		// Server -> one Peer: the info of all available pieces of a file
		System.out.println( "INFO: Network: Ask piece info  of file '" + fileID + "'" + " to " + clientSocket.getRemoteSocketAddress() );
		Message messSend = new Message( "GET", "VERIFY", fileID.toString() );
		out.println( messSend.toString() );
		waiting();
		this.server.notifyTimeOut();
		closeConnection();

	}
}
